﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace YQSH.DigitalCampus.Common
{
    /// <summary>
    /// 常用验证
    /// </summary>
    public class Validate
    {
        private static Regex booleanFormatRegex;

        #region 是否是E-Mail格式
        /// <summary>
        /// 是否是E-Mail格式
        /// </summary>
        /// <param name="value">要验证的值</param>
        /// <returns></returns>
        public static Boolean IsEMail(String value)
        {
            return Regex.IsMatch(value, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
        }
        #endregion

        #region 检测是否是正确的Url
        /// <summary>
        /// 检测是否是正确的Url
        /// </summary>
        /// <param name="value">要验证的Url</param>
        /// <returns>判断结果</returns>
        public static Boolean IsURL(String value)
        {
            return Regex.IsMatch(value, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|Int32|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
        }
        #endregion

        #region 判断是否为base64字符串
        /// <summary>
        /// 判断是否为base64字符串
        /// </summary>
        /// <param name="value">要验证的值</param>
        /// <returns></returns>
        public static Boolean IsBase64String(String value)
        {
            return Regex.IsMatch(value, @"[A-Za-z0-9\+\/\=]");
        }
        #endregion

        #region 检测是否有Sql危险字符
        /// <summary>
        /// 检测是否有Sql危险字符
        /// </summary>
        /// <param name="value">要判断字符串</param>
        /// <returns>判断结果</returns>
        public static Boolean IsSafeSqlString(String value)
        {
            return !Regex.IsMatch(value, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }
        #endregion

        #region 检测是否有危险的可能用于链接的字符串
        /// <summary>
        /// 检测是否有危险的可能用于链接的字符串
        /// </summary>
        /// <param name="value">要判断字符串</param>
        /// <returns>判断结果</returns>
        public static Boolean IsSafeUserInfoString(String value)
        {
            return !Regex.IsMatch(value, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest");
        }
        #endregion

        #region 判断是否为时间格式
        /// <summary>
        /// 判断是否为时间格式
        /// </summary>
        /// <param name="value">要验证的时间字符串</param>
        /// <returns></returns>
        public static Boolean IsTime(String value)
        {
            return Regex.IsMatch(value, @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
        }
        #endregion

        #region 判断字符串是否是yyyy-mm-dd字符串
        /// <summary>
        /// 判断字符串是否是yyyy-mm-dd字符串
        /// </summary>
        /// <param name="value">待判断字符串</param>
        /// <returns>判断结果</returns>
        public static Boolean IsDate(String value)
        {
            return Regex.IsMatch(value, @"(\d{4})-(\d{1,2})-(\d{1,2})");
        }
        #endregion

        #region 是否为IP
        /// <summary>
        /// 是否为IP
        /// </summary>
        /// <param name="value">要验证的值</param>
        /// <returns></returns>
        public static Boolean IsIP(String value)
        {
            return Regex.IsMatch(value, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
        #endregion

        #region 判断对象是否为Int32类型的数字
        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Boolean IsNumeric(object value)
        {
            if (value != null)
            {
                String str = value.ToString();
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                        return true;
                }
            }
            return false;
        }
        #endregion

        #region 是否是Double类型
        /// <summary>
        /// 是否是Double类型
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static Boolean IsDouble(object Expression)
        {
            if (Expression != null)
            {
                return Regex.IsMatch(Expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
            }
            return false;
        }
        #endregion

        #region 验证是否为正整数
        /// <summary>
        /// 验证是否为正整数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Boolean IsInt(String str)
        {
            return Regex.IsMatch(str, @"^[0-9]*$");
        }
        #endregion

        #region 判断一个字符串是否是有效的布尔值
        /// <summary>
        /// 判断一个字符串是否是有效的布尔值
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsBooleanFormat(string text)
        {
            if (booleanFormatRegex == null)
                booleanFormatRegex = new Regex("^(true|false)$", RegexOptions.IgnoreCase);
            return booleanFormatRegex.IsMatch(text);
        }
        #endregion

        #region 字段串是否为Null或为""(空)
        /// <summary>
        /// 字段串是否为Null或为""(空)
        /// </summary>
        /// <param name="value">要判断的值</param>
        /// <returns></returns>
        public static Boolean IsNullOrEmpty(String value)
        {
            if (value == null) return true;
            else return string.IsNullOrEmpty(value.Trim());
        }
        #endregion
    }
}
